﻿<?php 
error_reporting(0);
include "conn.php";
mysql_query('SET NAMES UTF8');

// $rert39=mysql_query("select *from goodsselect where goodsid='".$_REQUEST[goodsid]."' and title='".$_REQUEST[cc]."'and number='".$_REQUEST[cz]."'");
// $rowf7=mysql_fetch_array($rert39);
$rert30 = mysql_query("select * from goods where  goodsid ='" . $_REQUEST[goodsid] . "'");
$rowf = mysql_fetch_array($rert30);
$prize = array();
if ($_REQUEST[userid] != '') {
    $state = 0;
    if ($_REQUEST[fTitle]!='' && $_REQUEST[sTitle]!='') {
        $rert31 = mysql_query("select *from car where  goodsid ='" . $_REQUEST[goodsid] . "' and userid ='" . $_REQUEST[userid] . "'and state='" . $state . "' and fTitle='$_REQUEST[fTitle]' and sTitle='$_REQUEST[sTitle]'");
    }elseif ($_REQUEST[fTitle]!='') {
        $rert31 = mysql_query("select *from car where  goodsid ='" . $_REQUEST[goodsid] . "' and userid ='" . $_REQUEST[userid] . "'and state='" . $state . "' and fTitle='$_REQUEST[fTitle]'");
    }else{
        $rert31 = mysql_query("select *from car where  goodsid ='" . $_REQUEST[goodsid] . "' and userid ='" . $_REQUEST[userid] . "'and state='" . $state . "'");
    }
    
    $rowf2 = mysql_fetch_array($rert31);
    if ($rowf2 == true) {
        if ($_REQUEST[addnum] != '') {
            $addnum = $_REQUEST[addnum];
        } else {
            $addnum = 1;
        }
        //$srat = mysql_query("update car set number=number+'" . $addnum . "' where goodsid ='" . $_REQUEST[goodsid] . "' and userid ='" . $_REQUEST[userid] . "'and state='" . $state . "'");
       // $srat1 = mysql_query("update goods set number=number-'" . $addnum . "' where goodsid='" . $_REQUEST['goodsid'] . "'");
      //  $srat2 = mysql_query("update goods set xiaoliang=xiaoliang+'" . $addnum . "' where goodsid='" . $_REQUEST['goodsid'] . "'");
        $der = 200;
    } else {
        // $rert37=mysql_query("select *from goodsselect where goodsid='".$_REQUEST[goodsid]."' and title='".$_REQUEST[cc]."'and number='".$_REQUEST[cz]."'");
        // $rowf7=mysql_fetch_array($rert37);
        // $jiage = $_REQUEST[jiage];
        /* if ($_REQUEST[fTitle] == "") {
            $query = mysql_query("select * from goodsselect where goodsid=" . $_REQUEST[goodsid] . " order by selectid limit 1");
            while ($row = mysql_fetch_assoc($query)) {
                $goodsselect_row = $row;
            }
            $_REQUEST[fTitle] = $goodsselect_row[fTitle];
            $_REQUEST[sTitle] = $goodsselect_row[sTitle];
        }
        if ($_REQUEST[fTitle] != "" && $_REQUEST[sTitle] == "") {
            $query = mysql_query("select * from goodsselect where goodsid=" . $_REQUEST[goodsid] . " and fTitle='" . $_REQUEST[fTitle] . "' order by selectid limit 1");
            while ($row = mysql_fetch_assoc($query)) {
                $goodsselect_row = $row;
            }
            $_REQUEST[sTitle] = $goodsselect_row[sTitle];
        } */

		if ($_REQUEST[fTitle] != "" && $_REQUEST[sTitle] != "") {
            // 计算价格 
            $query = mysql_query("select price from goodsselect where goodsid=".$_REQUEST['goodsid']." and fTitle='".$_REQUEST[fTitle]."' and sTitle='".$_REQUEST[sTitle]."'");
            $goods = mysql_fetch_assoc($query);
            $price = $goods['price'];
            // 计算价格 end

            $fTitle=$_REQUEST[fTitle];
			$sTitle=$_REQUEST[sTitle];
        }else if ($_REQUEST[fTitle] != "" && $_REQUEST[sTitle] == "") {
            // 计算价格 
            $query = mysql_query("select price from goodsselect where goodsid=".$_REQUEST['goodsid']." and fTitle='".$_REQUEST[fTitle]."' ");
            $goods = mysql_fetch_assoc($query);
            $price = $goods['price'];
            // 计算价格 end

			$fTitle=$_REQUEST[fTitle];
			$sTitle='';
		}else{
            // 计算价格 
            $query = mysql_query("select price from goods where goodsid=".$_REQUEST['goodsid']);
            $goods = mysql_fetch_assoc($query);
            $price = $goods['price'];
            // 计算价格 end

			$fTitle='';
			$sTitle='';
		}
		$sql = "INSERT INTO car(userid,cityid,goodsid,goodsname,fTitle,sTitle,price,state,ordersid,`number`,shopid) VALUES ('{$_REQUEST['userid']}','{$_REQUEST['cityid']}','{$_REQUEST['goodsid']}','{$rowf['title']}','{$fTitle}','{$sTitle}','{$price}','{$state}','{$dingid}','{$_REQUEST['addnum']}','{$_REQUEST['shopid']}')";
        

        $srats = mysql_query($sql);
        if ($_REQUEST[addnum] != '') {
            $addnum = $_REQUEST[addnum];
        } else {
            $addnum = 1;
        }
      //  $srat1 = mysql_query("update goods set number=number-'" . $addnum . "' where goodsid='" . $_REQUEST['goodsid'] . "'");
        //$srat2 = mysql_query("update goods set xiaoliang=xiaoliang+'" . $addnum . "' where goodsid='" . $_REQUEST['goodsid'] . "'");
        //减少该商品规格的数量
        // if ($_REQUEST[sTitle] != '') {
        //     $srat3 = mysql_query("update goodsselect set number=number-'" . $addnum . "' where goodsid='" . $_REQUEST['goodsid'] . "' and fTitle='" . $_REQUEST[fTitle] . "' and sTitle='" . $_REQUEST[sTitle] . "'");
        // } else {
        //     $srat3 = mysql_query("update goodsselect set number=number-'" . $addnum . "' where goodsid='" . $_REQUEST['goodsid'] . "' and fTitle='" . $_REQUEST[fTitle] . "'");
        // }
        // if ($rowf[number] == 1) {
        //     $shu = 0;
        //     $srat1 = mysql_query("update goods set shangjia='" . $shu . "' where goodsid='" . $_REQUEST['goodsid'] . "'");
        // }
        // if ($srat1 == true) {
        //     $der = 200;
        // }
        $der = 200;
    }
} else {
    $der = 300;
}
$ret  = array('code' => $der);
echo json_encode($ret);
?>